package chan.project.smartlaweveryman.strategy.register;

import chan.project.smartlawcommon.common.ErrorCode;
import chan.project.smartlawcommon.exception.BusinessException;
import chan.project.smartlaweveryman.mapper.UserInfoMapper;
import chan.project.smartlawmodel.dto.common.CommonInfoRegisterRequest;
import chan.project.smartlawmodel.dto.user.UserInfoRegisterRequest;
import chan.project.smartlawmodel.entity.CommonInfo;
import chan.project.smartlawmodel.entity.UserInfo;
import chan.project.smartlawservice.commonService.CommonInfoFeignClient;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
public class CommonRegisterStrategy extends DefaultRegisterStrategy{

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private CommonInfoFeignClient commonInfoFeignClient;

    @Override
    public Long saveUser(UserInfoRegisterRequest userInfoRegisterRequest) {
        CommonInfoRegisterRequest commonInfoRegisterRequest = (CommonInfoRegisterRequest) userInfoRegisterRequest;
        //普通用户注册
        CommonInfo commonInfo = new CommonInfo();
        BeanUtils.copyProperties(commonInfoRegisterRequest,commonInfo);
        //获取用户id
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfoLambdaQueryWrapper.eq(UserInfo::getUserAccount,commonInfoRegisterRequest.getUserAccount());
        UserInfo userInfo = userInfoMapper.selectOne(userInfoLambdaQueryWrapper);
        commonInfo.setUserId(userInfo.getId());
        //调用服务进行普通用户注册
        boolean save = commonInfoFeignClient.commonRegister(commonInfo);
        if(!save){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"普通用户注册失败");
        }
        return userInfo.getId();
    }
}
